package com.lib76.socket.message;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.lib76.socket.model.SocketMessage;
import com.lib76.socket.model.protocol.SocketProtocolGroup;
import com.lib76.socket.model.protocol.SocketProtocolObject;
import com.lib76.socket.model.protocol.SocketProtocolParameter;

public class SpaceSocketMessage extends SocketMessage{

	private String[] buffer;
	private Integer pointer = 0; //����ָ��

	public SpaceSocketMessage() {
		super();
	}

	public SpaceSocketMessage(Object originalMessage) {
		super(originalMessage);
	}

	public SpaceSocketMessage(SocketProtocolObject protocolObject,
			Object originalMessage) {
		super(protocolObject, originalMessage);
	}

	public SpaceSocketMessage(SocketProtocolObject protocolObject) {
		super(protocolObject);
	}

	@Override
	public void read(List<SocketProtocolParameter> parameters) throws Exception {
		for(SocketProtocolParameter param : parameters){
			String value = buffer[pointer];
			this.data.put(param.getName(),changeDataType(param.getType(),value));
			if((++pointer) == buffer.length){return;}
		}
	}

	@Override
	public void write(List<SocketProtocolParameter> parameters)
			throws Exception {
		for(SocketProtocolParameter param : parameters){
			Object obj = this.data.get(param.getName());
			if(obj == null){obj = param.getValue();}
			if(obj == null){obj = "";}
			buffer[pointer] = obj.toString();
			pointer++;
		}
	}

	@Override
	public Object buildMessage() throws Exception {
		if(this.isOriginalMessageOutput == false && this.hasProtocol()){
			buffer = new String[2046];
			//���ò���
			this.data.put(SocketProtocolGroup.BODY_LENGTH_FIELD_NAME, this.protocolObject.getBodyLength());
			this.data.put(SocketProtocolGroup.MESSAGE_LENGTH_FIELD_NAME, this.protocolObject.getHeaderLength() + this.protocolObject.getBodyLength());
			this.data.put(SocketProtocolGroup.COMMAND_FIELD_NAME, this.protocolObject.getCommand());
			this.data.put(SocketProtocolGroup.DIRECTION_FIELD_NAME, this.protocolObject.getDirection());
			write(protocolObject.getHeaderProtocolParameter());
			write(protocolObject.getBodyFields());
			StringBuffer temp = new StringBuffer();
			for(int i=0;i<pointer;i++){
				temp.append(buffer[i]+",");
			}
			return temp.length() > 0?temp + "\n":null;
		}
		return originalMessage + "\n";
	}
	
	//=====================˽�з���========================//
	
	private Object changeDataType(String type,String value){
		if("Integer".equals(type)){
			return Integer.valueOf(value);
		}else if("Double".equals(type)){
			return Double.valueOf(value);
		}else{
			return value;
		}
	}

	@Override
	public void readOriginalMessage(Object originalMessage) {
		Pattern pat = Pattern.compile("([^,]*),");
		Matcher mat = pat.matcher(originalMessage.toString());
		buffer = new String[2046];
		int i = 0;
		while(mat.find()){
			buffer[i] = mat.group(1);
			i++;
		}
		if(this.hasProtocol()){
			try{
				read(protocolObject.getHeaderProtocolParameter());
				read(protocolObject.getBodyFields());
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}

}
